var form;
var table;
var layer;
var $;
var upload;
layui.use(['layer','form','table','upload'],function(){
    form = layui.form;
    table=layui.table;
    layer=layui.layer;
    $=layui.$;
    upload=layui.upload;

    ztree();

    showtables();

    /*$('#daoru').on('click',function(){
        upload.render({
            elem:'#daoru',
            url:'/OpeHome/addInsimportExcel',
            done:function(data){
                if(data.code==0){
                    layer.msg('上传成功');
                    console.log(data);
                }
            },
            drag:true,
            multiple:true,
        });
    })*/

    //导入
    /*upload.render({
        elem:'#daoru',
        url:'/OpeHome/addInsimportExcel',
        done:function(data){
            if(data.code==0){
                layer.msg('上传成功');
                console.log(data);
            }
        },
        drag:true,
        accept:'file',
        multiple:true,
    })*/


   /* upload.render({
        elem: '#daoru'
        ,url: '/OpeHome/addInsimportExcel' //此处配置你自己的上传接口即可
        ,accept: 'file' //普通文件
        ,done: function(res){
            layer.msg('上传成功');
            console.log(res);
        }
    });*/


})

//页面搜索框
var seek=function(){
    var productName=$('input[name=productName]').val();
    var condition=$('#condition option:selected').text();
    if(condition=="上架"){
        condition=1;
    }else if(condition=="下架"){
        condition=2;
    }else{
        condition=null;
    }
    var productLabel=$('input[name=productLabel]').val();
    $.post("/OpeHome/getTables", {"productName":productName,"condition":condition,"productLabel":productLabel}, function (data) {
        console.log(data.obj);
        tables(data.obj);
    });
}
//树形分类
function ztree(){
    $.post("/OpeHome/getZtree", function (data) {
        showZTree(data.obj);
    });
}
//展示树形分类
function showZTree(data) {
    $(document).ready(function(){
        var t=$("#ztree");
        var setting={
            view:{
                showLine:false,

            },
            data:{
                simpleData:{
                    enable:true,
                },
                key:{
                    name:"title",
                    idKey: "id",
                    pIdKey: "parentId",
                }
            },
        };
        $.fn.zTree.init(t,setting,data);
    })
}

//高级检索
function sousuo(){
    var code1=$("input[name='productCode1']").val();
    var name1=$("input[name='productName1']").val();
    var price=$("input[name='currentpriceProduct1']").val();
    var cate=$("input[name='categroyName1']").val();
    var comd=$("input[name='condition1']").val();
    var label=$("input[name='productLabel1']").val();

    $.post("/OpeHome/getTables",
        {"productCode":code1,"productName":name1,"currentpriceProduct":price,"categroyName":cate,"condition":comd,"productLabel":label},
        function (data) {
            console.log(data.obj);
            tables(data.obj);
        });
}
//高级检索弹出层
var search=function(){
    layer.open({
        type:2,  //弹出层类型
        title:"高级检索",
        area:['550px','410px'], //弹出层大小
        shade:0.4, //设置遮罩透明度
        offset:'auto', //设置遮罩位置
        content:'Operation_GoodsList_Search.html', //弹出层内容页面
        success: function (data) {
        }
    })
    window.layer_1=layer;
}

//展示table表格
function showtables(){
    $.post("/OpeHome/getTables",{"productSate":3,}, function (data) {
        tables(data.obj);
        console.log(data.obj);
    });
}
//table表格信息
function tables(data){
    console.log(data[0].main_image);
    table.render({
        elem:"#table_id",//容器
        data:data,
        title:"商品数据列表",
        limit:10,//分页显示条数
        toolbar:"#toolbar_id",
        page:true,
        cols:[[//定义列
            {type:'checkbox',align:'center',width:50},//复选框靠左
            {title:'序号',align:'center',templet:function(data){
                    return data.LAY_INDEX;
                }},
            {field:"product_code",title:"商品编号",align:'center',width:90},
            {field:"main_image",title:"商品图片",align:'center',width:160,templet:function(data){
                    return "<img src="+data.main_image+">"
                }},
            {field:"product_name",title:"商品名称",align:'center',width:160},
            {field:"currentprice_product",title:"商品价格",align:'center',width:90},
            {field:"state",title:"状态",align:'center',width:100,templet:function(data){
                    if(data.state==1){
                        return  "<input type='checkbox' checked=''  name='open' lay-skin='switch' lay-filter='switchTest'>"
                    }else if(data.state==2){
                        return  "<input type='checkbox' name='close' lay-skin='switch'>"
                    }
                }},
            {field:"categroy_name",title:"商品分类",align:'center',width:100},
            {field:"product_label",title:"商品标签",align:'center',width:100},
            {field:"product_sate",title:"审核状态",align:'center',width:110,templet:function(data){
                    if(data.product_sate==3){
                        return "<p style='margin-top:-10px;'>审核通过</p>" +
                            "<p style='margin-top:-50px;'><a onclick='javascript:auditDetails(\""+data.product_id+"\");' style='color:#1ABC9C;text-decoration: none;'>审核详情</a></p>"
                    }
                }},
            {field:"",title:"操作",align:'center',width:160,templet:function(data){
                    return "<a style='color:#009688;cursor:pointer;' onclick='javascript:check(\""+data.product_id+"\");'>查看</a>   " +
                        "<a style='color:#009688;cursor:pointer;' onclick='javascript:edit(\""+data.product_id+"\");'>编辑</a>   " +
                        "<a style='color:#009688;cursor:pointer;' onclick='javascript:operation(\""+data.product_id+"\");'>日志</a> "+
                        "<a style='color:#009688;cursor:pointer;' onclick='delProduct(\""+data.product_id+"\");'>删除</a>  " ;
                }}
        ]]
    });
}
//审核详情
var auditDetails=function(id){
    layer.open({
        type:2,  //弹出层类型
        title:"商品详情",
        area:['810px','450px'], //弹出层大小
        shade:0.4, //设置遮罩透明度
        offset:'auto', //设置遮罩位置
        content:'Operation_GoodsList_AuditDetails.html', //弹出层内容页面
        success:function(layero,index){
            //查找当前引入的iframe内容
            var obj=layero.find("iframe").contents();
            //用选择器查找  选中input框
            obj.find('input[name=productId]').val(id);
        }
    })
}
//查看商品详情
var check=function(id){
    $.post("/OpeHome/getTables",{"productId":id}, function (data) {
        console.log(data.obj);
        details(data.obj);
    });
}
//商品详情弹出层
function details(data){
    layer.open({
        type:2,  //弹出层类型
        title:"商品详情",
        data:data,
        area:['650px','400px'], //弹出层大小
        shade:0.4, //设置遮罩透明度
        offset:'auto', //设置遮罩位置
        content:'Operation_GoodsList_Check.html', //弹出层内容页面
        success:function(layero,index){
            //查找当前引入的iframe内容
            var obj=layero.find("iframe").contents();

            if(data[0].state==1){
                data[0].state="上架";
            }else if(data[0].state==2){
                data[0].state="下架";
            }
            //用选择器查找  选中input框
            obj.find('input[name=productCode]').val(data[0].product_code);
            obj.find('input[name=productName]').val(data[0].product_name);
            obj.find('input[name=currentpriceProduct]').val(data[0].currentprice_product);
            obj.find('input[name=condition]').val(data[0].state);
            obj.find('input[name=categoryId]').val(data[0].categroy_name);
            obj.find('input[name=productLabel]').val(data[0].product_label);
            obj.find('textarea[name=productDescription]').val(data[0].product_description);
        }
    })
}
//当前商品id
var productId;
//修改商品信息
var edit=function(id){
    $.post("/OpeHome/getTables",{"productId":id}, function (data) {
        edits(data.obj);
    });
    productId=$("input[name='productId1']").val(id);
}

//修改商品信息2
var edits=function(data){
    layer.open({
        type:2,  //弹出层类型
        title:"修改商品信息",
        area:['650px','410px'], //弹出层大小
        shade:0.4, //设置遮罩透明度
        offset:'auto', //设置遮罩位置
        content:'Operation_GoodsList_Edit.html', //弹出层内容页面
        success:function(layero,index){
            //查找当前引入的iframe内容
            var obj=layero.find("iframe").contents();
            if(data[0].state==1){
                data[0].state="上架";
            }else if(data[0].state==2){
                data[0].state="下架";
            }
            //用选择器查找  选中input框
            obj.find('input[name=productCode]').val(data[0].product_code);
            obj.find('input[name=productName]').val(data[0].product_name);
            obj.find('input[name=currentpriceProduct]').val(data[0].currentprice_product);
            obj.find('input[name=condition]').val(data[0].state);
            obj.find('input[name=categoryId]').val(data[0].categroy_name);
            obj.find('input[name=productLabel]').val(data[0].product_label);
            obj.find('textarea[name=productDescription]').val(data[0].product_description);
        },
    });
    window.layer_1=layer;
}
//修改商品信息3
function Modify(){
    var name1=$("input[name='productName1']").val();
    var price=$("input[name='currentpriceProduct1']").val();
    var comd=$("input[name='condition1']").val();
    var label=$("input[name='productLabel1']").val();
    var prodesc1=$("input[name='productDescription1']").val();
    var productId1=$("input[name='productId1']").val();
    console.log(comd);

    $.post("/OpeHome/setProduct",
        {"productName":name1,"currentpriceProduct":price,"state":comd,"productLabel":label,"productDescription":prodesc1,"productId":productId1},
        function (data) {
            window.location.reload();
        });
}
//操作日志
var operation=function(id){
    layer.open({
        type:2,  //弹出层类型
        title:"商品操作日志",
        area:['1010px','450px'], //弹出层大小
        shade:0.4, //设置遮罩透明度
        offset:'auto', //设置遮罩位置
        content:'Operation_GoodsList_operation.html', //弹出层内容页面
        success:function(layero,index){
            //查找当前引入的iframe内容
            var obj=layero.find("iframe").contents();
            //用选择器查找  选中input框
            obj.find('input[name=productId]').val(id);
        }
    })
}

//删除商品信息
delProduct=function(id){
    $.post("/OpeHome/reProduct",{"productId":id}, function (data) {
        window.location.reload();
    });
}
//导入

//刷新
$("#refreshs").on('click',function(){
    window.location.reload();
})
//返回上一级
$("#back").on('click',function(){
    history.back(-1);
})